package com.alu4r.atcrowdfunding.manager.controller;

import com.alu4r.atcrowdfunding.VO.Data;
import com.alu4r.atcrowdfunding.bean.User;
import com.alu4r.atcrowdfunding.manager.service.UserService;
import com.alu4r.atcrowdfunding.util.JsonMessageResult;
import com.alu4r.atcrowdfunding.util.StringUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;
    //分配角色
    @ResponseBody
    @RequestMapping(value = "/doAssignRole",method = RequestMethod.POST)
    public JsonMessageResult AssignRole(Integer userId, Data data){

        JsonMessageResult jsonMessageResult;
        boolean isSuccess = userService.insertUserRoleRelationship(userId,data);
        if(isSuccess){
            jsonMessageResult = JsonMessageResult.success();
        }else{
            jsonMessageResult = JsonMessageResult.fail();
        }
        return jsonMessageResult;
    }

    //移除角色
    @ResponseBody
    @RequestMapping(value = "/doNnAssignRole",method = RequestMethod.POST)
    public JsonMessageResult unAssignRole(Integer userId, Data data){
        System.out.println("移除角色");
        for(Integer i : data.getIds()){
            System.out.println(i+"#########################");
        }
        JsonMessageResult jsonMessageResult;
        boolean isSuccess = userService.deleteUserRoleRelationship(userId,data);
        if(isSuccess){
            jsonMessageResult = JsonMessageResult.success();
        }else{
            jsonMessageResult = JsonMessageResult.fail();
        }
        return jsonMessageResult;
    }

    @ResponseBody
    @RequestMapping(value = "/deleteBatch",method = RequestMethod.POST)
    public JsonMessageResult deleteBatch(Integer[] id){
        System.out.println("批量删除");
        JsonMessageResult jsonMessageResult;
        boolean isSuccess = userService.deleteBatch(id);
        if(isSuccess){
            jsonMessageResult = JsonMessageResult.success();
        }else{
            jsonMessageResult = JsonMessageResult.fail();
        }
        return jsonMessageResult;
    }

    @ResponseBody
    @RequestMapping(value = "/deleteUser",method = RequestMethod.POST)
    public JsonMessageResult deleteUser(@RequestParam(value = "userId") Integer id){
        System.out.println("删除一组数据"+id);
        JsonMessageResult jsonMessageResult;
        boolean isSuccess = userService.deleteUser(id);
        if(isSuccess){
            jsonMessageResult = JsonMessageResult.success();
        }else{
            jsonMessageResult = JsonMessageResult.fail();
        }
        return jsonMessageResult;
    }

    @ResponseBody
    @RequestMapping(value = "/updateUser",method = RequestMethod.POST)
    public JsonMessageResult update(User user) {
        System.out.println("修改数据：");
        JsonMessageResult jsonMessageResult;
        boolean isSuccess = userService.updateUser(user);
        System.out.println(isSuccess);
        if(isSuccess){
            jsonMessageResult = JsonMessageResult.success();
        }else {
            jsonMessageResult = JsonMessageResult.fail();
        }
        return jsonMessageResult;
    }


    @ResponseBody
    @RequestMapping("/doAdd")
    public JsonMessageResult add(User user){
        JsonMessageResult jsonMessageResult;
        boolean isSuccess = userService.insertUser(user);
        if(isSuccess){
            jsonMessageResult = JsonMessageResult.success();
        }else {
            jsonMessageResult = JsonMessageResult.fail();
        }
        return jsonMessageResult;
    }


    //异步请求
    @ResponseBody
    @RequestMapping("/index")
    public JsonMessageResult index(@RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                                   @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize,
                                   @RequestParam(value = "queryText",required = false, defaultValue = "") String queryText ){
        System.out.println("查询全部user");
        Map<String, Object> map = new HashMap<>();
        map.put("pageNum",pageNum);
        map.put("pageSize",pageSize);
        System.out.println(queryText);
        if(StringUtil.isNotEmpty(queryText)){
            if(queryText.contains("%")){
                queryText = queryText.replaceAll("%","\\\\%");
            }
            map.put("queryText",queryText);
        }
        PageInfo pageInfo = userService.getPage(map);
        return JsonMessageResult.success().add("pageInfo",pageInfo);
    }

}
